package com.valkyries.jhhj.service.impl;

import cn.dev33.satoken.stp.StpInterface;
import com.valkyries.jhhj.domain.Permission;
import com.valkyries.jhhj.domain.Role;
import com.valkyries.jhhj.domain.UserPermission;
import com.valkyries.jhhj.domain.UserRole;
import com.valkyries.jhhj.service.PermissionService;
import com.valkyries.jhhj.service.RoleService;
import com.valkyries.jhhj.service.UserPermissionService;
import com.valkyries.jhhj.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 自定义权限验证接口扩展
 */

@Component
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private UserPermissionService userPermissionService;
    @Autowired
    private PermissionService permissionService;

    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleService roleService;

    /**
     * @return 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<Long> permissionIds = userPermissionService.queryListByLoginId(loginId).stream()
                .map(UserPermission::getPermissionId)
                .toList();
        return permissionService.queryListByIds(permissionIds).stream()
                .map(Permission::getPermissionName)
                .toList();
    }

    /**
     * @return 返回一个账号所拥有的角色标识集合
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<Long> roleIds = userRoleService.queryListByLoginId(loginId).stream()
                .map(UserRole::getRoleId)
                .toList();
        return roleService.queryListByIds(roleIds).stream()
                .map(Role::getRoleName)
                .toList();
    }
}
